<template>
  <div class="layout-container">
    <!-- 左侧联系人列表 -->
    <div class="left-sidebar">
      <h2 class="sidebar-title">联系人列表</h2>
      <el-list class="contact-list">
        <el-list-item
          v-for="contact in contactStore.contacts"
          :key="contact.id"
          @click="chatStore.selectContact(contact.id)"
          class="contact-item"
        >
          <div class="contact-info">
            <el-avatar :size="40" :src="contact.avatar" />
            <div class="contact-details">
              <div class="name">{{ contact.name }}</div>
              <div class="last-msg">{{ contact.lastMessage }}</div>
            </div>
            <div class="time">{{ formatTime(contact.lastMessageTime) }}</div>
          </div>
        </el-list-item>
      </el-list>
    </div>

    <!-- 右侧主内容区 -->
    <div class="main-content">
      <!-- 聊天区域 -->
      <div v-if="chatStore.selectedContact" class="chat-section">
        <h2 class="chat-title">{{ 当前联系人名称 }}</h2>
        <div class="chat-messages">
          <!-- 消息列表保持不变 -->
        </div>
        <div class="chat-input">
          <!-- 输入框保持不变 -->
        </div>
      </div>
      <div v-else class="empty-chat">
        <p>请选择一个联系人开始聊天</p>
      </div>
    </div>
  </div>
</template>
<script setup>
import { useContactStore } from '../stores/contactStore'
import { useChatStore } from '../stores/chatStore'
import { formatDistanceToNow } from 'date-fns'
 
const contactStore = useContactStore()
const chatStore = useChatStore()
 
// 时间格式化方法
const formatTime = (timeString) => {
  return formatDistanceToNow(new Date(timeString), { addSuffix: true })
}
 
// 修改发送消息方法
const sendMessage = () => {
  if (inputText.value.trim() && chatStore.selectedContact) {
    // 发送消息逻辑
    chatStore.sendMessage(chatStore.selectedContact, inputText.value)
    
    // 更新联系人最后消息
    contactStore.updateLastMessage(
      chatStore.selectedContact,
      inputText.value
    )
    
    inputText.value = ''
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  display: flex;
  height: 100vh; /* 确保全屏高度 */

  .left-sidebar {
    width: 350px; /* 加宽左侧栏 */
    background: #f8f9fa;
    border-right: 1px solid #e9ecef;
    display: flex;
    flex-direction: column;
    
    .sidebar-title {
      padding: 20px;
      margin: 0;
      border-bottom: 1px solid #e9ecef;
    }

    .contact-list {
      flex: 1;
      overflow-y: auto;
      
      .contact-item {
  padding: 12px 20px;
  transition: all 0.3s;
 
  &:hover {
    background-color: #f5f7fa;
  }
 
  .contact-info {
    display: flex;
    align-items: center;
    width: 100%;
 
    .contact-details {
      flex: 1;
      margin-left: 12px;
      min-width: 0;
 
      .name {
        font-weight: 500;
        color: #303133;
        margin-bottom: 4px;
      }
 
      .last-msg {
        font-size: 12px;
        color: #909399;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
 
    .time {
      font-size: 12px;
      color: #c0c4cc;
      margin-left: 8px;
    }
  }
}

    }
  }

  .main-content {
    flex: 1;
    display: flex;
    background: white;

    .chat-section {
      flex: 1;
      display: flex;
      flex-direction: column;
      max-width: calc(100% - 350px); /* 同步左侧宽度 */

      .chat-title {
        padding: 20px;
        margin: 0;
        border-bottom: 1px solid #e9ecef;
        background: #f8f9fa;
      }

      .chat-messages {
        flex: 1;
        overflow-y: auto;
        padding: 20px;
        background: #ffffff;
        
        .chat-message {
          margin-bottom: 15px;
          
          .message-content {
            max-width: 60%;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
          }
        }
      }

      .chat-input {
        padding: 20px;
        background: #f8f9fa;
        border-top: 1px solid #e9ecef;
        
        .message-input {
          border-radius: 8px;
        }
      }
    }

    .empty-chat {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 18px;
      color: #868e96;
    }
  }
}
</style>